using System;
using System.Xml;

namespace LocalBoard.Domain
{
	/// <summary>
	/// Summary description for Board.
	/// </summary>
	public class Board
	{
		public Board(int id, string name, Profile manager, int accessRoleId, int postRoleId, int clubId, BoardViewType viewType, String boardData) : this(id, name, manager, accessRoleId, postRoleId, clubId, viewType)
		{
			_dataXml = new XmlDocument();
			_dataXml.LoadXml(boardData);
		}
		public Board(int id, string name, Profile manager, int accessRoleId, int postRoleId, int clubId, BoardViewType viewType) : this(name, manager, accessRoleId, postRoleId, clubId, viewType)
		{
			_id = id;
		}
		public Board(string name, Profile manager, int accessRoleId, int postRoleId, int clubId, BoardViewType viewType, String customData)
			: this(name, manager, accessRoleId, postRoleId, clubId, viewType)
		{
			_dataXml = new XmlDocument();
			_dataXml.LoadXml(customData);
		}
		public Board(string name, Profile manager, int accessRoleId, int postRoleId, int clubId, BoardViewType viewType)
		{
			_name = name;
			_manager = manager;
			_accessRoleId = accessRoleId;
			_postRoleId = postRoleId;
			_clubId = clubId;
			_viewType = viewType;
		}
		private int _id;
		private string _name;
		private Profile _manager;
		private int _accessRoleId;
		private int _postRoleId;
		private int _clubId;
		private BoardViewType _viewType;

		public int Id
		{
			get{return _id;}
		}
		public string Name
		{
			get{return _name;}
			set{_name = value;}
		}
		public Profile Manager
		{
			get{return _manager;}
			set{_manager = value;}
		}
		public int AccessRoleId
		{
			get{return _accessRoleId;}
			set{_accessRoleId = value;}
		}
		public int PostRoleId
		{
			get{return _postRoleId;}
			set{_postRoleId = value;}
		}
		public int ClubId
		{
			get{return _clubId;}
		}
		public BoardViewType ViewType
		{
			get{return _viewType;}
			set{_viewType = value;}
		}
		string _latitude;
		public string Latitude
		{
			get
			{
				if (_latitude == null)
				{
					if (_dataXml != null)
					{
						XmlNode node = _dataXml.SelectSingleNode("/Board/@latitude");
						if (node != null)
							_latitude = node.Value;
					}
					if (_latitude == null)
						_latitude = "";
				}
				return _latitude;
			}
			set {
				_latitude = value;
				SetAttributeValue("latitude", value);
			}
		}
		string _longitude;
		public string Longitude
		{
			get
			{
				if (_longitude == null)
				{
					if (_dataXml != null)
					{
						XmlNode node = _dataXml.SelectSingleNode("/Board/@longitude");
						if (node != null)
							_longitude = node.Value;
					}
					if (_longitude == null)
						_longitude = "";
				}
				return _longitude;
			}
			set
			{
				_longitude = value;
				SetAttributeValue("longitude", value);
			}
		}
		string _placeName;
		public string PlaceName
		{
			get
			{
				if (_placeName == null)
				{
					if (_dataXml != null)
					{
						XmlNode node = _dataXml.SelectSingleNode("/Board/@placeName");
						if (node != null)
							_placeName = node.Value;
					}
					if (_placeName == null)
						_placeName = "";
				}
				return _placeName;
			}
			set
			{
				_placeName = value;
				SetAttributeValue("placeName", value);
			}
		}
		string _region;
		public string Region
		{
			get
			{
				if (_region == null)
				{
					if (_dataXml != null)
					{
						XmlNode node = _dataXml.SelectSingleNode("/Board/@region");
						if (node != null)
							_region = node.Value;
					}
					if (_region == null)
						_region = "";
				}
				return _region;
			}
			set
			{
				_region = value;
				SetAttributeValue("region", value);
			}
		}
		string _address;
		public string Address
		{
			get
			{
				if (_address == null)
				{
					if (_dataXml != null)
					{
						XmlNode node = _dataXml.SelectSingleNode("/Board/@address");
						if (node != null)
							_address = node.Value;
					}
					if (_address == null)
						_address = "";
				}
				return _address;
			}
			set
			{
				_address = value;
				SetAttributeValue("address", value);
			}
		}
		string _city;
		public string City
		{
			get
			{
				if (_city == null)
				{
					if (_dataXml != null)
					{
						XmlNode node = _dataXml.SelectSingleNode("/Board/@city");
						if (node != null)
							_city = node.Value;
					}
					if (_city == null)
						_city = "";
				}
				return _city;
			}
			set
			{
				_city = value;
				SetAttributeValue("city", value);
			}
		}
		string _state;
		public string State
		{
			get
			{
				if (_state == null)
				{
					if (_dataXml != null)
					{
						XmlNode node = _dataXml.SelectSingleNode("/Board/@state");
						if (node != null)
							_state = node.Value;
					}
					if (_state == null)
						_state = "";
				}
				return _state;
			}
			set
			{
				_state = value;
				SetAttributeValue("state", value);
			}
		}
		string _postalCode;
		public string PostalCode
		{
			get
			{
				if (_postalCode == null)
				{
					if (_dataXml != null)
					{
						XmlNode node = _dataXml.SelectSingleNode("/Board/@postalCode");
						if (node != null)
							_postalCode = node.Value;
					}
					if (_postalCode == null)
						_postalCode = "";
				}
				return _postalCode;
			}
			set
			{
				_postalCode = value;
				SetAttributeValue("postalCode", value);
			}
		}
		XmlDocument _dataXml = new XmlDocument();
		public XmlDocument DataXml
		{
			get { return _dataXml; }
		}

		private void SetAttributeValue(string attributeName, string value)
		{
			XmlNode node = _dataXml.SelectSingleNode("/Board/@" + attributeName);
			if (node == null)
			{
				XmlElement element = GetDocumentElement();
				XmlAttribute attr = _dataXml.CreateAttribute(attributeName);
				element.Attributes.Append(attr);
				node = attr;
			}
			node.Value = value;
		}

		private XmlElement GetDocumentElement()
		{
			XmlElement element = _dataXml.DocumentElement;
			if (element == null)
			{
				element = _dataXml.CreateElement("Board");
				_dataXml.AppendChild(element);
			}
			return element;
		}

		#region static memeber
		public static int GetBoardViewTypeId(BoardViewType viewType)
		{
			switch(viewType)
			{
				case BoardViewType.Weblog:
					return 1;
				case BoardViewType.Album:
					return 2;
				case BoardViewType.List:
				default:
					return 3;
			}
		}
		public static BoardViewType GetBoardViewType(int viewTypeId)
		{
			switch(viewTypeId)
			{
				case 1:
					return BoardViewType.Weblog;
				case 2:
					return BoardViewType.Album;
				case 3:
				default:
					return BoardViewType.List;
			}
		}
		#endregion
	}
}